//
//  CHDropViewController.m
//  CHDropViewDemo
//
//  Created by hyc on 16/9/7.
//  Copyright © 2016年 hyc. All rights reserved.
//

#import "CHDropViewController.h"

@interface CHDropViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property(nonatomic,strong)UITableView *topDropTableView;
@property(nonatomic,strong)UITableView *secDropTableView;

@property(nonatomic,assign)CGFloat maxTopViewHeight;
@property(nonatomic,assign)CGFloat maxSecViewHeight;

@end

@implementation CHDropViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //一级
    self.topDropTableView = [[UITableView alloc] init];
    if (self.topViewFrame.size.width==0) {
        self.topDropTableView.frame = self.view.bounds;
    }else{
        _topDropTableView.frame = _topViewFrame;
    }
    self.topDropTableView.delegate = self;
    self.topDropTableView.dataSource = self;
    [self.view addSubview:self.topDropTableView];
    self.topDropTableView.tableFooterView = [[UIView alloc] init];
    self.topDropTableView.bounces = NO;
    self.maxTopViewHeight = self.topDropTableView.frame.size.height;
    
    //二级
    self.secDropTableView = [[UITableView alloc] initWithFrame:CGRectMake(CH_WIDTH/2, 0, CH_WIDTH/2, CH_HEIGHT)];
    if (self.secViewFrame.size.width==0) {
        self.secDropTableView.frame = CGRectMake(CH_WIDTH/2, 0, CH_WIDTH/2, CH_HEIGHT);
    }else{
        _secDropTableView.frame = _secViewFrame;
    }
    self.secDropTableView.delegate = self;
    self.secDropTableView.dataSource = self;
    [self.view addSubview:self.secDropTableView];
    self.secDropTableView.tableFooterView = [[UIView alloc] init];
    self.secDropTableView.bounces = NO;
    self.maxSecViewHeight = self.secDropTableView.frame.size.height;
    
    //初始化设置
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (_bgColor) {
        self.view.backgroundColor = _bgColor;
    }else{
        self.view.backgroundColor = [UIColor clearColor];
    }
    if (_topBgColor) {
        _topDropTableView.backgroundColor = _topBgColor;
    }
    if (_secBgColor) {
        _secDropTableView.backgroundColor = _secBgColor;
    }
    if (_topViewSelectCellAtRow>=100) {//大于100则证明设置过这个值
        [self dropView:_topDropTableView selectCellAtRow:_topViewSelectCellAtRow-100];
    }
    if (_secViewSelectCellAtRow>=100) {//大于100则证明设置过这个值
        [self dropView:_secDropTableView selectCellAtRow:_secViewSelectCellAtRow-100];
    }
    
    if (_pushStyle) {
        self.view.backgroundColor = [UIColor whiteColor];
    }else{
        self.view.backgroundColor = [UIColor clearColor];
    }
    _topDropTableView.hidden = _topHidden;
    _secDropTableView.hidden = _secHidden;

    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self endReload];
    [self builtSelectArr];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self buildView];
    }
    return self;
}

- (void)buildView{
    //present的时候如果不设置这个值背景将覆盖了父控制器
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    //动画效果
    self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
}

- (void)builtSelectArr{
}

#pragma mark - tableView

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.topDropTableView) {
        NSInteger num = 0;
        if (self.numberOfTopCell) {
            num = self.numberOfTopCell(tableView);
        }
        return num;
    }else{
        NSInteger num = 0;
        if (self.numberOfSecCell) {
            num = self.numberOfSecCell(tableView);
        }
        return num;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.topDropTableView){
        UITableViewCell *cell = nil;
        if (self.topCell) {
            cell = self.topCell(tableView,indexPath.row);
            if (self.topCellColor) {
                cell.backgroundColor = self.topCellColor;
            }
            if (self.topSelectCellColor) {
                cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
                cell.selectedBackgroundView.backgroundColor = self.topSelectCellColor;
            }
        }
        if (cell==nil) {
            cell = [[UITableViewCell alloc] init];
        }
        return cell;
    }else{
        UITableViewCell *cell = nil;
        if (self.secCell) {
            cell = self.secCell(tableView,indexPath.row);
            if (self.secCellColor) {
                cell.backgroundColor = self.secCellColor;
            }
            if (self.secSelectCellColor) {
                cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
                cell.selectedBackgroundView.backgroundColor = self.secSelectCellColor;
            }

        }
        if (cell==nil) {
            cell = [[UITableViewCell alloc] init];
        }
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.topDropTableView){
        if (self.selectTopCell) {
            self.selectTopCell(indexPath.row);
        }
    }else{
        if (self.selectSecCell) {
            self.selectSecCell(indexPath.row);
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==self.topDropTableView) {
        CGFloat height = 44;
        if (self.topCellHeight) {
            self.topCellHeight(tableView,indexPath.row);
        }
        return height;
    }else{
        CGFloat height = 44;
        if (self.secCellHeight) {
            self.secCellHeight(tableView,indexPath.row);
        }
        return height;
    }
}

#pragma mark - scroll

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView==self.topDropTableView){
        if (self.topViewDidScroll) {
            self.topViewDidScroll(scrollView);
        }
    }else{
        if (self.secViewDidScroll) {
            self.secViewDidScroll(scrollView);
        }
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self goBack];
}

- (void)goBack{
    if (self.navigationController) {//push方式
        
    }else{
        if (self.parentViewController) {//add方式
            [self.view removeFromSuperview];
        }else{//present
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
}

#pragma mark - 刷新

- (void)reloadTopData{
    [self.topDropTableView reloadData];
    [self endReload];
}

- (void)reloadSecData{
    [self.secDropTableView reloadData];
    [self endReload];
}

- (void)reloadData{
    [self reloadTopData];
    [self reloadSecData];
}

- (void)endReload{
    dispatch_async(dispatch_get_main_queue(), ^{
        //刷新结束
        if (self.viewAutoSize) {
            [self updateDropView:_topDropTableView maxHeight:_maxTopViewHeight];
            [self updateDropView:_secDropTableView maxHeight:_maxSecViewHeight];
        }
        if (self.topViewAutoSize) {
            [self updateDropView:_topDropTableView maxHeight:_maxTopViewHeight];
        }
        if (self.secViewAutoSize) {
           [self updateDropView:_secDropTableView maxHeight:_maxSecViewHeight];
        }
        if (_keepTopAndSecAlignBottom) {
            if (_topDropTableView.frame.size.height<_secDropTableView.frame.size.height) {
                CGRect frame = _topDropTableView.frame;
                frame.size.height = _secDropTableView.frame.size.height;
                _topDropTableView.frame = frame;
            }else{
                CGRect frame = _secDropTableView.frame;
                frame.size.height = _topDropTableView.frame.size.height;
                _secDropTableView.frame = frame;
            }
        }
    });
}

- (void)updateDropView:(UITableView *)tableView maxHeight:(CGFloat)height{
    CGRect frame = tableView.frame;
    if (height>=tableView.contentSize.height) {
        frame.size.height = tableView.contentSize.height;
    }else{
        frame.size.height = height;
    }
    tableView.frame = frame;
}

#pragma mark - 颜色

- (void)setBgColor:(UIColor *)bgColor{
    _bgColor = bgColor;
    self.view.backgroundColor =bgColor;
}

- (void)setTopBgColor:(UIColor *)topBgColor{
    _topBgColor = topBgColor;
    self.topDropTableView.backgroundColor = topBgColor;
}

- (void)setSecBgColor:(UIColor *)secBgColor{
    _secBgColor = secBgColor;
    self.secDropTableView.backgroundColor = secBgColor;
}

#pragma mark - 菜单的frame
- (void)setTopViewFrame:(CGRect)topViewFrame{
    _topViewFrame = topViewFrame;
    _topDropTableView.frame = _topViewFrame;
}
- (void)setSecViewFrame:(CGRect)secViewFrame{
    _secViewFrame = secViewFrame;
    _secDropTableView.frame = _secViewFrame;
}

#pragma mark - 默认选中

- (void)dropView:(UITableView *)tableView selectCellAtRow:(NSInteger)row{
    NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:row inSection:0];
    [tableView selectRowAtIndexPath:selectedIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

- (void)setTopViewSelectCellAtRow:(NSInteger)topViewSelectCellAtRow{
    _topViewSelectCellAtRow = topViewSelectCellAtRow+100;
    [self dropView:_topDropTableView selectCellAtRow:topViewSelectCellAtRow];
}

- (void)setSecViewSelectCellAtRow:(NSInteger)secViewSelectCellAtRow{
    _secViewSelectCellAtRow = secViewSelectCellAtRow+100;
    [self dropView:_secDropTableView selectCellAtRow:secViewSelectCellAtRow];
}

#pragma mark - 其他

- (void)setTopBounces:(BOOL)topBounces{
    _topBounces = topBounces;
    self.topDropTableView.bounces = topBounces;
}

- (void)setSecBounces:(BOOL)secBounces{
    _secBounces = secBounces;
    self.secDropTableView.bounces = secBounces;
}

- (void)setPushStyle:(BOOL)pushStyle{
    _pushStyle = pushStyle;
    if (pushStyle) {
       self.view.backgroundColor = [UIColor whiteColor];
    }else{
       self.view.backgroundColor = [UIColor clearColor];
    }
    
    _secDropTableView.hidden = YES;
}

- (void)setTopHidden:(BOOL)topHidden{
    _topHidden = topHidden;
    _topDropTableView.hidden = topHidden;
}

- (void)setSecHidden:(BOOL)secHidden{
    _secHidden = secHidden;
    _secDropTableView.hidden = secHidden;
}

@end
